<template>
	<view class="container">
		<!-- 订单状态切换 -->
		<view class="status-tab">
			<view class="tab-item" v-for="(item, index) in statusList" :key="index"
				:class="{ active: currentStatus === item.value }" @click="switchStatus(item.value)">
				{{ item.label }}
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list">
			<view class="order-item" v-for="(order, index) in orderList" :key="index">
				<view class="order-header">
					<text class="order-no">订单号：{{ orderList.details_order_number }}</text>
					<text class="order-status" :class="'status-' + orderList.status">{{ getStatusText(orderList.status)
					}}</text>
				</view>
				<view class="order-goods">
					<image class="goods-image" :src="orderList.details_photo" mode="aspectFill"></image>
					<view class="goods-info">
						<view class="goods-name">{{ orderList.details_name }}</view>
						<view class="goods-spec">
							<text class="original-price">原价：¥{{ (orderList.details_price*1.2).toFixed(2)}}</text>
						</view>
						<view class="goods-price-qty">
							<text class="goods-price">优惠价：¥{{ orderList.details_price  }}</text>
						</view>
					</view>
				</view>
				<view class="order-footer">
					<view class="total">
						合计:
						<text class="price">¥{{ orderList.details_price }}</text>
					</view>
				</view>
			</view>
			<!-- 空状态 -->
			<view class="empty-state" v-if="orderList.length === 0">
				<image src="/static/empty-order.png" mode="aspectFit"></image>
				<text>暂无相关订单</text>
			</view>
		</view>
	</view>
</template>

<script>
import { orderApi } from '@/api/index';

export default {
	data() {
		return {
			currentStatus: 'all',
			statusList: [
				{ label: '全部', value: 'all' },
				{ label: '待付款', value: 'unpaid' },
				{ label: '待发货', value: 'unshipped' },
				{ label: '待收货', value: 'shipped' },
				{ label: '待评价', value: 'received' }
			],
			orderList: [],
			page: 1,
			pageSize: 10,
			hasMore: true,
			loading: false
		}
	},
	onLoad(options) {
		if (options.type) {
			this.currentStatus = options.type;
		}
		this.getOrderList(true);
	},
	onPullDownRefresh() {
		this.page = 1;
		this.getOrderList(true);
	},
	onReachBottom() {
		if (this.hasMore) {
			this.page++;
			this.getOrderList();
		}
	},
	methods: {
		// 切换订单状态
		switchStatus(status) {
			console.log('切换状态:', status);
			if (status !== this.currentStatus) {
				this.currentStatus = status;
				this.page = 1;
				this.getOrderList();
			}
		},

		// 获取订单列表
		getOrderList() {
			console.log('获取订单列表，当前状态:', this.currentStatus);
			console.log('当前页码:', this.page);
			
			uni.request({
				url: 'https://m1.apifoxmock.com/m1/6102433-5793108-default/admin/order/1',
				method: 'GET',
				data: {
					page: this.page,
					pageSize: this.pageSize,
					status: this.currentStatus !== 'all' ? this.currentStatus : null
				},
				success: (res) => {
					console.log('请求参数:', {
						page: this.page,
						pageSize: this.pageSize,
						status: this.currentStatus
					});
					
					let data = res.data || res;
					console.log('接口返回原始数据:', res);
					console.log('处理后的数据:', data);
					console.log('订单列表数据:', data.order_msg);
					
					this.orderList = data.order_msg;
					console.log('设置后的orderList:', this.orderList);
				},
				fail: (err) => {
					console.error('获取数据错误:', err);
				},
			})
		},

		// 获取状态文本
		getStatusText(status) {
			console.log('获取状态文本，当前状态:', status);
			const result = status || '未知状态';
			console.log('状态文本结果:', result);
			return result;
		},

		// 计算商品总数量
		getTotalQuantity(goods) {
			return goods.reduce((total, item) => total + item.quantity, 0);
		},


		// 将数字状态转为字符串
		getStatusFromNumber(statusNum) {
			// 假设0=unpaid，1=unshipped，2=shipped，3=received，4=completed，5=cancelled
			const map = ['unpaid', 'unshipped', 'shipped', 'received', 'completed', 'cancelled'];
			return map[statusNum] || 'unknown';
		}
	}
}
</script>

<style lang="scss">
.container {
	background-color: #f8f8f8;
	min-height: 100vh;
}

.status-tab {
	display: flex;
	background-color: #fff;
	padding: 0 20rpx;
	border-bottom: 1rpx solid #eee;
	position: sticky;
	top: 0;
	z-index: 10;

	.tab-item {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;
		color: #333;
		position: relative;

		&.active {
			color: #FF5151;
			font-weight: bold;

			&:after {
				content: '';
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translateX(-50%);
				width: 60rpx;
				height: 4rpx;
				background-color: #FF5151;
			}
		}
	}
}

.order-list {
	padding: 20rpx;

	.order-item {
		background-color: #fff;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		padding: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

		.order-header {
			display: flex;
			justify-content: space-between;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #f5f5f5;

			.order-no {
				font-size: 24rpx;
				color: #666;
			}

			.order-status {
				font-size: 24rpx;

				&.status-unpaid {
					color: #FF5151;
				}

				&.status-unshipped,
				&.status-shipped {
					color: #FFA500;
				}

				&.status-received {
					color: #1AAD19;
				}

				&.status-completed {
					color: #999;
				}

				&.status-cancelled {
					color: #999;
				}
			}
		}

		.order-goods {
			display: flex;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

			.goods-image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 8rpx;
				margin-right: 20rpx;
				background-color: #f5f5f5;
			}

			.goods-info {
				flex: 1;
				position: relative;

				.goods-name {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 10rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.goods-spec {
					font-size: 24rpx;
					color: #999;
					margin-bottom: 10rpx;

					.original-price {
						text-decoration: line-through;
						color: #999;
					}
				}

				.goods-price-qty {
					display: flex;
					justify-content: space-between;

					.goods-price {
						font-size: 28rpx;
						color: #FF5151;
						font-weight: bold;
					}

					.goods-qty {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}

		.order-footer {
			padding-top: 20rpx;

			.total {
				text-align: right;
				font-size: 24rpx;
				color: #666;
				margin-bottom: 20rpx;

				.price {
					color: #FF5151;
					font-weight: bold;
					font-size: 28rpx;
				}
			}

			.action-buttons {
				display: flex;
				justify-content: flex-end;

				.btn {
					padding: 0 30rpx;
					height: 60rpx;
					line-height: 60rpx;
					border-radius: 30rpx;
					background-color: #FF5151;
					color: #fff;
					font-size: 24rpx;
					margin-left: 20rpx;

					&.outline {
						background-color: #fff;
						color: #666;
						border: 1rpx solid #ddd;
					}
				}
			}
		}
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;

		image {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 20rpx;
		}

		text {
			font-size: 28rpx;
			color: #999;
		}
	}
}
</style>